﻿using IFRS.DataAccess.DAL;
using IFRS.Models;
using IFRS.Web;
using IFRS_Global;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Threading;
using System.Web.UI;

public partial class IFRS_WIN30_4A : PageBase
{
    #region "---------- Events -----------"

    protected void Page_Load(object sender, EventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        if (!(Page.IsPostBack || Page.IsCallback))
        {
            AddClientScript();
            this.ViewMenu();
            this.BindDDL();
            this.SetErrorMessage();
            if (Request.QueryString["id"] != null)
            {
                this.BindData(Convert.ToInt32(Request.QueryString["id"].ToString()));
            }
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }

    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData();
    }
    protected void btnSAVE_NEW_Click(object sender, EventArgs e)
    {
        SaveData(true);
    }
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN30_1S.aspx");

    }   
    protected void btnCLEAR_Click(object sender, EventArgs e)
    {
        Response.Redirect(ResolveUrl(Request.Url.AbsolutePath));
    }

    #endregion "---------- Events -----------"

    #region "----------  Methods -----------"

    private void AddClientScript()
    {
        btnCANCEL.Attributes["OnClick"] = "if(!confirm(\"" + Resources.ifrsResource.ConfirmCancel + "\")) return false;";
    }

    private void ViewMenu()
    {
        MenuTitleBar = Resources.ifrsMenu.WIN30_4A;
    }

    private void SetErrorMessage()
    {
        btnSAVE.ValidationGroup = "Master";
        btnSAVE_NEW.ValidationGroup = "Master";
        reqTransectionDate.SetRequiredField(lblTransectionDate, txtTransectionDate, btnSAVE);
        reqIndustryType.SetRequiredField(lblIndustryType, ddlIndustryType, btnSAVE);
        reqFairValue.SetRequiredField(lblFairValue, txtFairValue, btnSAVE);
        
    }

    private void BindData(int ID)
    {
        List<InvestmentFairValue> investmentFairValue = new List<InvestmentFairValue>();
        DataTable dt = new SPR_INVESTMENTFAIRVALUE().SEARCH_BYID(ID);
        if (dt.Rows.Count > 0)
        {
            ConvertHelper.ConvertDataTableToObject(dt, investmentFairValue);
            txtTransectionDate.Text = DateTime.Now.DateToString();
            ddlIndustryType.SelectedValue = investmentFairValue[0].IndustryTypeID.ToString();
            txtFairValue.Text = investmentFairValue[0].FairValue.DecimalToString();         
        }
    }

    private void SaveData(bool SaveNew = false)
    {
        try
        {
            InvestmentFairValue investmentFairValue = new InvestmentFairValue();
            investmentFairValue.TransactionDate = txtTransectionDate.Text.StringToDateAllowNull();
            investmentFairValue.IndustryTypeID = ddlIndustryType.SelectedValue.AsIntIsnull();
            investmentFairValue.FairValue = txtFairValue.Text.AsDecimal() ;
            investmentFairValue.SetStandardField();

            int iID = new SPR_INVESTMENTFAIRVALUE().INSERT(investmentFairValue);

            if (iID > 0)
            {
                if (SaveNew)
                {
                    this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(Request.Url.AbsolutePath));
                }
                else
                {
                    this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN30_2E.aspx?&id={0}", iID)));
                }
            }
            else
            {
                if (iID == -1)
                {
                    Utility.AlertMessage(Page, Resources.ifrsResource.Duplicate);
                }
                else
                {
                    Utility.AlertMessage(Page, Resources.ifrsResource.CanNotSaveData);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void BindDDL()
    {
        ddlIndustryType.SetData(DDL_TYPE.IndustryType, "", false, true);
    }

    #endregion "----------  Methods -----------"
}